﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Utilities.Encoders;
using System.Net;
using up6.db.model;
using up6.utils;
using Up6.core.Up6Manager;
using Up6.core.Up6Manager.Models.Req;


namespace Up6.core.Controllers;

[ApiController]
[Route("")]
public class Up6NoSqlController : ControllerBase
{
    private readonly Up6NoSqlAction act;
    private readonly IConfiguration _configuration;
    public Up6NoSqlController(Up6NoSqlAction up6Service, IConfiguration configuration)
    {
        act = up6Service;
        _configuration = configuration;
    }

    [HttpGet]
    [Route("api/up6/nosql/f_create")]
    public async Task<IActionResult> createAsync(
        [FromQuery] string id,
        [FromQuery] string uid,
        [FromQuery] string md5,
        [FromQuery] long lenLoc,
        [FromQuery] long lenLocSec,
        [FromQuery] string sizeLoc,
        [FromQuery] string callback,
        [FromQuery] string pathLoc,
        [FromQuery] int blockSizeSec=0,
        [FromQuery] int blockSize = 5242880,
        [FromQuery] string encryptAgo="",
        [FromQuery] string token="",
        [FromQuery] string pid = "",
        [FromQuery] string pidRoot = ""
        )
    {
        var f = new FileInf();
        f.pid= pid;
        f.pidRoot= pidRoot;
        f.md5 = md5;
        f.id= id;
        f.uid= uid;
        f.lenLoc= lenLoc;
        f.lenLocSec= lenLocSec;
        f.sizeLoc = sizeLoc;
        f.blockSize = blockSize;//块大小
        f.blockSizeSec = blockSizeSec;//加密块大小
        f.encryptAgo = encryptAgo;//加密算法
        f.encrypt = ConfigReader.storageEncrypt();//存储加密
        f.token = token;
        f.callback = callback;
        f.pathLoc = pathLoc;
        f.pathLoc = WebUtility.UrlDecode(pathLoc);
        f.nameLoc = Path.GetFileName(f.pathLoc);
        f.nameSvr = f.nameLoc;
        return await act.CreateAsync(f);

    }

    [HttpGet]
    [Route("api/up6/nosql/f_complete")]

    public dynamic complete(
        [FromQuery] string id,
        [FromQuery] string uid,
        [FromQuery] string callback
        )
    {
        var f = new FileInf();
        f.id = id;
        f.uid= uid;
        f.callback = callback;
        return act.complete(f);

    }

    [HttpGet]
    [Route("api/up6/nosql/f_del")]

    public dynamic del(
        [FromQuery] string id,
        [FromQuery] string uid,
        [FromQuery] string callback
        )
    {
        var f = new FileInf();
        f.id= id;
        f.uid= uid;
        f.callback= callback;
        return act.del(f);

    }

    [HttpGet]
    [Route("api/up6/nosql/f_list")]

    public dynamic list(
        [FromQuery] string uid,
        [FromQuery] string callback
        )
    {
        var f = new FileInf();
        f.uid= uid;
        f.callback = callback;
        return act.list(f);

    }

    [HttpGet]
    [Route("api/up6/nosql/f_process")]

    public dynamic process(
        [FromQuery] string id,
        [FromQuery] string uid,
        [FromQuery] long offset,
        [FromQuery] long lenSvr,
        [FromQuery] string perSvr,
        [FromQuery] string callback
        )
    {
        var f = new FileInf();
        f.id= id;
        f.uid = uid;
        f.offset= offset;
        f.lenSvr= lenSvr;
        f.perSvr= perSvr;
        f.callback = callback;
        return act.process(f);

    }

    [HttpGet]
    [Route("api/up6/nosql/fd_create")]

    public dynamic fd_create(
        [FromQuery] string id,
        [FromQuery] string uid,
        [FromQuery] long lenLoc,
        [FromQuery] string sizeLoc,
        [FromQuery] string pathLoc,
        [FromQuery] string callback,
        [FromQuery] string pid = "",
        [FromQuery] string pidRoot = ""
        )
    {
        var f = new FileInf();
        f.id= id;
        f.pid= pid;
        f.pidRoot= pidRoot;
        f.fdTask = true;
        f.uid = uid;
        f.pathLoc=WebUtility.UrlDecode(pathLoc);
        f.nameLoc = Path.GetFileName(f.pathLoc);
        f.nameSvr = f.nameLoc;
        f.lenLoc= lenLoc;
        f.callback= callback;
        return act.fd_create(f);
    }

    [HttpGet]
    [Route("api/up6/nosql/fd_complete")]

    public dynamic fd_complete(
        [FromQuery] string id,
        [FromQuery] string uid,
        [FromQuery] string callback
        )
    {
        var f = new FileInf();
        f.id = id;
        f.uid = uid;
        f.callback = callback;
        return act.fd_complete(f);
    }

    [HttpGet]
    [Route("api/up6/nosql/fd_del")]

    public dynamic fd_del(
        [FromQuery] string id,
        [FromQuery] string uid,
        [FromQuery] string callback
        )
    {
        var f = new FileInf();
        f.id = id;
        f.uid = uid;
        f.callback = callback;
        return act.fd_del(f);

    }
}
